package org.raymond.icloud.common.core.model;

import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import org.raymond.icloud.common.core.constants.ErrorCode;

import java.io.Serializable;

@Data
@ApiModel(value="接口返回对象", description="接口返回对象")
public class Result<T> implements Serializable {
    private static final long serialVersionUID = 1L;

    @ApiModelProperty(value = "成功标志")
    private boolean success = false;

    @ApiModelProperty(value = "返回处理消息")
    private String message = "操作成功！";

    @ApiModelProperty(value = "返回处理详情")
    private String detailMessage;

    @ApiModelProperty(value = "返回代码")
    private Integer code = 0;

    @ApiModelProperty(value = "返回数据对象")
    private T data;

    @ApiModelProperty(value = "资源路径")
    private String uri;

    @ApiModelProperty(value = "资源操作")
    private String method;

    @ApiModelProperty(value = "时间戳")
    private long timestamp = System.currentTimeMillis();

    private Result() {
    }

    public static<T> Result<T> success(){
        return success("成功", null);
    }

    public static<T> Result<T> success(T data) {
        return success("成功", data);
    }

    public static<T> Result<T> success(String msg, T data) {
        Result<T> r = new Result<>();
        r.setMessage(msg);
        r.setData(data);
        r.setCode(ErrorCode.OK.getCode());
        r.setSuccess(true);
        return r;
    }

    public static<T> Result<T> error(String msg) {
        return error(ErrorCode.ERROR.getCode(), msg);
    }

    public static<T> Result<T> error(int code, String msg) {
        Result<T> r = new Result<>();
        r.setCode(code);
        r.setMessage(msg);
        r.setSuccess(false);
        return r;
    }

    public Result<T> detail(String detailMessage){
        this.detailMessage = detailMessage;
        return this;
    }

    public Result<T> uri(String uri){
        this.uri = uri;
        return this;
    }

    public Result<T> method(String method){
        this.method = method;
        return this;
    }
}
